<?php

namespace app\common\controller;

use think\Controller;
/**
 * 微信服务商
 */
class SubWxPay extends Controller
{
    /**
     * 拉起支付
     *
     * @return payUrl
     */
    public function toPay($orderInfo,$config,$trade_type = 'MWEB'){
        $data = array(
            'appid'     => $config['sub_appid'],
            'mch_id'    => $config['sub_mchid'],
            'sub_mch_id'=> $config['sub_mch_id'],
            'device_info'=> 'WEB',
            'nonce_str' => $orderInfo['pay_order_number'],
            'profit_sharing' => 'Y',
            'body'      => $orderInfo['props_name'],
            'out_trade_no'  => $orderInfo['pay_order_number'],
            'total_fee' => $orderInfo['pay_amount'] * 100,
            'spbill_create_ip'  => $orderInfo['order_ip'],
            'notify_url'    => $orderInfo['notify_url'],
            'trade_type'    => $trade_type,
            'scene_info'    => '{"h5_info":"h5_info"}',
            'product_id'    => $orderInfo['product_id']
        );
        $data['sign'] = $this->getSign($data,$config['sub_key'],'md5');
        $xml = data_to_xml($data);
        $res = getHttpPostJson('https://api.mch.weixin.qq.com/pay/unifiedorder',$xml);
        $xml = simplexml_load_string($res,"SimpleXMLElement", LIBXML_NOCDATA);
        $xml= json_encode($xml);  //objecct转json
        $xml = json_decode($xml,true); //json转array
        return $xml;
    }
    /**
     * 添加分账收款方
     *
     * @return
     */
    public function subAccount($params){
        $data = array(
            'mch_id'    => $params['sub_mchid'],
            'sub_mch_id'=> $params['sub_mch_id'],
            'appid'     => $params['sub_appid'],
            'nonce_str' => date('Ymd') . date('His') . sp_random_string(8),
        );
        $receiver = array(
            'type'  => "MERCHANT_ID",
            'account'=> $params['sub_mchid'],
            'name'  => $params['sub_mch_name'],
            'relation_type' => 'SERVICE_PROVIDER'
        );
        $data['receiver'] = json_encode($receiver);
        $data['sign'] = $this->getSign($data,$params['sub_key']);
        $xml = data_to_xml($data);
        $res = getHttpPostJson('https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver',$xml);
        $xml = simplexml_load_string($res,"SimpleXMLElement", LIBXML_NOCDATA);
        $xml= json_encode($xml);  //objecct转json
        $xml=json_decode($xml,true); //json转array
        return $xml;
    }

    public function getSign($data,$subKey,$signType='hash_hmac'){
        ksort($data);
        $str = http_build_query($data);
        $stringSign = urldecode($str).'&key='.$subKey;
        if($signType == 'hash_hmac'){
            $sign = hash_hmac("sha256",$stringSign,$subKey);
        }else{
            $sign = md5($stringSign);
        }
        return strtoupper($sign);
    }

    public function OrderQuery($config,$pay_order_number){
        $arr = array(
            'appid'     => $config['sub_appid'],
            'mch_id'    => $config['sub_mchid'],
            'sub_mch_id'=> $config['sub_mch_id'],
            'out_trade_no'  => $pay_order_number,
            'nonce_str' => date('Ymd') . date('His') . sp_random_string(4),
        );
        $arr['sign'] = $this->getSign($arr,$config['sub_key'],'md5');
        $xml = data_to_xml($arr);
        $res = getHttpPostJson('https://api.mch.weixin.qq.com/pay/orderquery',$xml);
        $xml = simplexml_load_string($res,"SimpleXMLElement", LIBXML_NOCDATA);
        $xml = json_encode($xml);
        $xml = json_decode($xml,true);
        return $xml;
    }
}
